package com.lcf.framework.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.lcf.base.exception.SysException;
import com.lcf.framework.config.enums.BaseEnum;
import lombok.Getter;

import java.util.Optional;

/**
 * 字典状态1启用0停用
 *
 * @author lcf
 */
@Getter
public enum SysDictStatusEnum implements BaseEnum<Integer> {
    ENABLE(1, "启用"),
    DISABLE(0, "停用");

    @EnumValue
    @JsonValue
    private final Integer code;
    private final String name;

    SysDictStatusEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public static SysDictStatusEnum getEnum(Integer code) {
        for (SysDictStatusEnum item : values()) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return null;
    }

    public static SysDictStatusEnum checkCode(Integer code) {
        return Optional.ofNullable(getEnum(code)).orElseThrow(() -> new SysException("字典状态错误"));
    }
}
